package com.erp.auto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erp.auto.domain.ErpAutoViolationRecord;
import com.erp.auto.mapper.ErpAutoViolationRecordMapper;
import com.erp.auto.service.ErpAutoViolationRecordService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 自动化违规记录
 *
 * @author WXX
 * @date 2024-01-11 09:41:30
 */
@Service
@AllArgsConstructor
public class ErpAutoViolationRecordServiceImpl implements ErpAutoViolationRecordService {

    private final ErpAutoViolationRecordMapper erpAutoViolationRecordMapper;

    @Override
    public int saveViolationRecord(ErpAutoViolationRecord record) {
        ErpAutoViolationRecord erpAutoViolationRecord = erpAutoViolationRecordMapper.selectById(record);
        if (erpAutoViolationRecord == null) {
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            return erpAutoViolationRecordMapper.insert(record);
        }
        record.setUpdateTime(new Date());
        return erpAutoViolationRecordMapper.updateById(record);
    }

    @Override
    public List<ErpAutoViolationRecord> getList(ErpAutoViolationRecord record) {
        LambdaQueryWrapper<ErpAutoViolationRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (record.getStoreCn() != null) {
            queryWrapper.eq(ErpAutoViolationRecord::getStoreCn, record.getStoreCn());
        }
        return erpAutoViolationRecordMapper.selectList(queryWrapper);
    }

    @Override
    public ErpAutoViolationRecord selectById(Long id) {

        return erpAutoViolationRecordMapper.selectById(id);
    }

    @Override
    public List<ErpAutoViolationRecord> getViolationUnExecutedList() {
        return erpAutoViolationRecordMapper.selectList(new LambdaQueryWrapper<ErpAutoViolationRecord>()
                .eq(ErpAutoViolationRecord::getIsDownShelf, 0)
                .like(ErpAutoViolationRecord::getViolationType, "侵权"));
    }
}
